#include "collaboration_subsystem.h"
#include "MI_client.h"

namespace collaboration {

EventHandler* EVENT_HANDLER; //definicion de la variable extern event_handler
MIServiceConnector* MI_SERVICE_CONNECTOR; //definicion de la variable extern mi_service_connector
MI_client* mi_client;
CORBA::ORB_ptr orb;

bool setup_environment(int argc, char** argv)
{
	try{
		orb = CORBA::ORB_init(argc, argv);
		EVENT_HANDLER = new EventHandler(orb);
		mi_client = new MI_client(orb);
	}
	catch(const CORBA::Exception&)
	{
		return false;
	}
	
	return true;
}

void shutdown_environment()
{
	delete EVENT_HANDLER;
	delete mi_client;
	
	orb->destroy();
}

bool setup_MIServiceConnector(const char* app, const char* user, const char* password)
{
	if ( (EVENT_HANDLER != NULL) && (mi_client != NULL) )
	{
		MI_SERVICE_CONNECTOR = new MIServiceConnector(app, user, password, 
													EVENT_HANDLER, mi_client);
													
		return true;
	}
	else
	{
		return false;
	}
}

void shutdown_MIServiceConnector()
{
	delete MI_SERVICE_CONNECTOR;
} 

ListOfApplications* get_applications()
{
	if (mi_client != NULL)
		return mi_client->applications();
		
	else return NULL;
}

ListOfUsers* get_users(const char* app)
{
	if (mi_client != NULL)
		return mi_client->users(app);
		
	else return NULL;
}

}